﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Ue.Infrastructure.DomainBase;
using Ue.Infrastructure.RepositoryFramework;
using Ue.Infrastructure.RepositoryFramework.Repository;
using Ue.Qcdz.Domain.Medias;

using Ue.Qcdz.Domain.Tags;
using Ue.Qcdz.UI.Controls.Medias;

namespace Ue.Qcdz.Repositories.Medias
{
    public class MediaRepository : SqlServerRepository<TheMedia>, IMediaRepository
    {
        #region Overrides of SqlServerRepositoryBase<TheMedia>

        protected override string GetDataTableName()
        {
            return "Medias";
        }
        protected override string GetKeyFieldName()
        {
            return MediaFactory.FieldNames.EntityKey;
        }

        protected override string[] GetBaseInsertFields()
        {
            return new string[]
                       {
                           MediaFactory.FieldNames.EntityKey,
                           MediaFactory.FieldNames.MediaImage,
                           MediaFactory.FieldNames.MediaLink,
                           MediaFactory.FieldNames.MediaName,
                           MediaFactory.FieldNames.MediaType
        };
        }

        protected override string[] GetBaseUpdateFields()
        {
            return new string[]
                       {
                           MediaFactory.FieldNames.MediaImage,
                           MediaFactory.FieldNames.MediaLink,
                           MediaFactory.FieldNames.MediaName,
                           MediaFactory.FieldNames.MediaType
                        };
        }

        protected override IDataParameter[] BuildParameters(TheMedia entity)
        {
            return new IDataParameter[]
                       {
                           RepositoryDriver.BuildParameter(MediaFactory.FieldNames.EntityKey,entity.Key),
                           RepositoryDriver.BuildParameter(MediaFactory.FieldNames.MediaImage,entity.MediaImage),
                           RepositoryDriver.BuildParameter(MediaFactory.FieldNames.MediaLink,entity.MediaLink),
                           RepositoryDriver.BuildParameter(MediaFactory.FieldNames.MediaName,entity.MediaName),
                           RepositoryDriver.BuildParameter(MediaFactory.FieldNames.MediaType,entity.MediaType.Key)
                       };
        }

        protected override IWhereClause BuildWhereClauseBy(IDataQuery dataQuery)
        {
            return null;
        }

        protected override IDictionary<string, ChildDataHandler> GetBuildEntityChildCallbacks()
        {
            var callBacks = new Dictionary<string, SqlServerRepository<TheMedia>.ChildDataHandler>();
            callBacks.Add(MediaFactory.FieldNames.MediaType, delegate(TheMedia media, object fieldValue)
            {

                if (media != null)
                {
                    ITagRepository tagRepository = RepositoryFactory.GetRepository<ITagRepository, Tag>();

                    media.MediaType = tagRepository[fieldValue];
                }
            });

            return callBacks;
        }
        
        #endregion

        #region Implementation of IMediaRepository

        //public IList<TheMedia> FindBy(Tag type)
        //{
        //    var query = "SELECT * FROM Medias WHERE typeId=@typeId";
        //    var parameter = new IDataParameter[]
        //                        {
        //                            this.RepositoryDriver.BuildParameter(MediaFactory.FieldNames.MediaType, type.Key)
        //                        };
        //    return this.BuildEntitiesFromSql(query, parameter);
        //}

 
        #endregion

        public IList<TheMedia> FindBy(string typeId)
        {
            var query = "SELECT * FROM Medias WHERE typeId=@typeId";
            var parameter = new IDataParameter[]
                                {
                                    this.RepositoryDriver.BuildParameter(MediaFactory.FieldNames.MediaType,typeId)
                                };
            return this.BuildEntitiesFromSql(query, parameter);
        }

        public IList<TheMedia> FindBy()
        {
            var query = "SELECT Medias.*,Tags.Name FROM Medias INNER JOIN  Tags ON typeId=Tags.Id";
            return this.BuildEntitiesFromSql(query);
        }
    }
}
